/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.uca.sisbec.actions.becario;

import com.uca.sisbec.commons.HibernateAction;
import com.uca.sisbec.forms.becario.AsistenciaReunionForm;
import com.uca.sisbec.forms.becario.MotivoReunionForm;
import com.uca.sisbec.helpers.becario.AsistenciaBecariosHelper;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.Transaction;

/**
 *
 * @author Jorgore
 */
public class AsistenciaBecariosAction extends HibernateAction {

    public ActionForward init(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        setOptionsList(request);
        return mapping.findForward("success");
    }

    public ActionForward seleccionar(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        AsistenciaReunionForm arf = (AsistenciaReunionForm) form;
        AsistenciaBecariosHelper helper = new AsistenciaBecariosHelper(getSession(request));
        request.setAttribute("reunionBean", helper.getReunionById(arf.getReunion()));
        List<MotivoReunionForm> becXreu = helper.getBecariosXReunion(arf.getReunion());
        if (becXreu.isEmpty()){
            helper.setBecariosXReunionDefault(arf.getReunion());
            becXreu = helper.getBecariosXReunion(arf.getReunion());
        }
        arf.getMotivo().clearAndAddAll(becXreu);
        request.setAttribute("becariosList", becXreu);
        setOptionsList(request);
        return mapping.findForward("success");
    }

    public ActionForward guardar(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        AsistenciaReunionForm arf = (AsistenciaReunionForm) form;
        AsistenciaBecariosHelper helper = new AsistenciaBecariosHelper(getSession(request));
        
        int rows = helper.eliminarAsistencia(arf.getReunion());
        
        /* Reinicio de transaccion */
        getTx(request).commit();
        Transaction tx = getSession(request).beginTransaction();
        request.getSession().setAttribute("_TRX_HIB", tx);
        
        arf.getMotivo().setInitialShow(rows);
        arf.getMotivo().setCurrentlyShowing(rows);
        helper.guardarAsistencia(arf);
        
        List<MotivoReunionForm> becXreu = helper.getBecariosXReunion(arf.getReunion());
        arf.getMotivo().clearAndAddAll(becXreu);
        request.setAttribute("becariosList", becXreu);
        request.setAttribute("reunionBean", helper.getReunionById(arf.getReunion()));
        setOptionsList(request);
        return mapping.findForward("success");
    }

    private void setOptionsList(HttpServletRequest request) {
        AsistenciaBecariosHelper helper = new AsistenciaBecariosHelper(getSession(request));
        request.setAttribute("reunionesList", helper.getReunionesPasadas());
    }
}
